package com.yqg.education.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.yqg.education.entity.ExamLog;
import org.apache.ibatis.annotations.MapKey;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author 张芸博
 * @since 2025-04-07
 */
@Mapper
public interface ExamLogMapper extends BaseMapper<ExamLog> {

    List<ExamLog> findPage(Integer pageNum, Integer pageSize, String username, Integer trainClassId, Integer courseId, Integer isPass, Integer stuId);

    Long findCount(String username, Integer trainClassId, Integer courseId, Integer isPass, Integer stuId);




    /**
     * 学员端考试未完成查询
     */
    @MapKey("id")
    List<Map<String,Object>> findAllAndCompletedExamPage(@Param("pageStartNumber") Integer pageStartNumber,
                                             @Param("pageSize") Integer pageSize,
                                             @Param("name") String name,
                                             @Param("typeId") Integer typeId,
                                             @Param("courseId") List<Long> courseId,
                                             @Param("userId") Integer userId,
                                             @Param("flow") Integer flow);

    Integer findAllAndCompletedExamPageCount(@Param("name") String name,
                                 @Param("typeId") Integer typeId,
                                 @Param("courseId") List<Long> courseId,
                                 @Param("userId") Integer userId,
                                 @Param("flow") Integer flow);

    /**
     * 学员端考试已完成查询
     */
    @MapKey("courseId")
    List<Map<String, Object>> findIncompleteExamPage(@Param("pageStartNumber") Integer pageStartNumber,
                                                     @Param("pageSize") Integer pageSize,
                                                     @Param("name") String name,
                                                     @Param("typeId") Integer typeId,
                                                     @Param("courseId") List<Long> courseId,
                                                     @Param("userId") Integer userId,
                                                     @Param("flow") Integer flow);

    Integer findIncompleteExamPageCount(@Param("name") String name,
                                        @Param("typeId") Integer typeId,
                                        @Param("courseId") List<Long> courseId,
                                        @Param("userId") Integer userId,
                                        @Param("flow") Integer flow);



    /**
     * 查询排名
     */
//    @Select("SELECT rank FROM (SELECT stu_id, exampaper_get_score, @current_rank := IF(exampaper_get_score = @previous_score, @current_rank, @current_rank + @count) AS rank, @count := IF(exampaper_get_score = @previous_score, @count + 1, 1), @previous_score := exampaper_get_score FROM (select * from exam_log where course_id = #{courseId} and is_delete = 0) as exam_log, (SELECT @current_rank := 1, @previous_score := NULL, @count := 0) vars ORDER BY exampaper_get_score DESC) AS ranked WHERE exampaper_get_score = #{exampaperGetScore} and stu_id = #{userId};")
    Integer getRank(@Param("exampaperGetScore") BigDecimal exampaperGetScore,
                    @Param("userId") Integer userId,
                    @Param("courseId") Integer courseId);
    /**
     * 根据课程ID和学员ID查询成绩,配合排名来处理查看成绩接口
     */
    ExamLog findExamScore(@Param("userId") Integer userId,
                          @Param("courseId") Integer courseId);

    Integer findExamAccuracy(@Param("userId") Integer userId,
                             @Param("exampaperId") Integer exampaperId);

    Long stuFindIsExamSign(@Param("courseId") Integer courseId,
                            @Param("stuId") Integer stuId);

    ExamLog stuFindExamLogByStuIdAndCourseId(@Param("stuId") Long stuId,
                                             @Param("courseId") Long courseId);

    /**
     * 根据学员ID获取一批考试信息
     * @param stuIdList 学员ID列表
     */
    List<ExamLog> getInfoByStuId(@Param("stuIdList") List<Long> stuIdList);
}
